package com.meitianhui.convenitenbuy.module.order.ui.fragment;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.base.BaseFragment;
import com.meitianhui.convenitenbuy.main.model.bean.EventBean;
import com.meitianhui.convenitenbuy.module.order.adapter.BPPOrderAdapter;
import com.meitianhui.convenitenbuy.module.order.contract.EdinburghTypeOrderContract;
import com.meitianhui.convenitenbuy.module.order.di.DaggerOrderComponent;
import com.meitianhui.convenitenbuy.module.order.di.OrderModule;
import com.meitianhui.convenitenbuy.module.order.model.bean.OrderInfo;
import com.meitianhui.convenitenbuy.module.order.presenter.BPPOrderPresenter;
import com.meitianhui.convenitenbuy.util.RxBus;
import com.meitianhui.convenitenbuy.util.TipUtils;
import com.meitianhui.convenitenbuy.widget.dialog.CancelOrderDialog;
import com.meitianhui.convenitenbuy.widget.dialog.CustomTipDialog;
import com.meitianhui.convenitenbuy.widget.dialog.LoadingDialog;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 积分兑 (Bonus Points Payment) 订单的页面。
 * Created by Liam on 2018/3/16.
 */

public class BPPOrderFragment extends BaseFragment
        implements EdinburghTypeOrderContract.View, SwipeRefreshLayout.OnRefreshListener {

    public static final String Tag = BPPOrderFragment.class.getSimpleName();
    private String mService = "gdActivity.consumer.jifenOrderListPageFind";
    private int mPageIndex = 1;
    private int mPageSize = 15;
    private BPPOrderAdapter mAdapter;
    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeLayout;
    private View mNotDataView;
    private LoadingDialog mDialog;
    private CustomTipDialog mODialog = null;
    private CancelOrderDialog mCancelDialog;
    private int mOrderStatus = 0;   //0 取消订单  1 提交订单
    @Inject
    BPPOrderPresenter presenter;

    @Override
    protected int getLayout() {
        return R.layout.fragment_bonus_points_order;
    }

    @Override
    protected void initView(LayoutInflater inflater) {
        super.initView(inflater);
        DaggerOrderComponent.builder()
                .appComponent(App.getInstance().getAppComponent())
                .orderModule(new OrderModule(this))
                .build()
                .inject(this);
        mSwipeLayout = (SwipeRefreshLayout) getViewById(R.id.srl_bpp_swipe_layout);
        mSwipeLayout.setOnRefreshListener(this);
        mRecyclerView = (RecyclerView) getViewById(R.id.rv_bpp_order_list);
        LinearLayoutManager lManager = new LinearLayoutManager(getActivity());
        lManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(lManager);
        mNotDataView = getActivity().getLayoutInflater().inflate(R.layout.data_empty_order,
                (ViewGroup) mRecyclerView.getParent(), false);
        initData();
        registerEvent();
    }

    private void initData() {
        mAdapter = new BPPOrderAdapter(getActivity(), null, null);
        mRecyclerView.setAdapter(mAdapter);
        onRefresh();
    }

    private void registerEvent(){
        Disposable subscribe = RxBus.get().toFlowable(EventBean.class).subscribe(new Consumer<EventBean>() {
            @Override
            public void accept(EventBean eventBean) throws Exception {
                if (eventBean.type == Constants.EVENT_TYPE_LOGIN_SUCCESS) {
                    presenter.getOrder(mService, "1", mPageSize + "", "", "");
                }
            }
        });
        mCompositeDisposable.add(subscribe);
    }

    @Override
    public void onRefresh() {
        mPageIndex = 1;
        mDialog = new LoadingDialog(getActivity());
        presenter.getOrder(mService, mPageIndex + "", mPageSize + "", "", "");
    }

    @Override
    public void showError(String msg) {
    }

    @Override
    public void getOrderSuccess(OrderInfo orderInfo) {
        mSwipeLayout.setRefreshing(false);
        if (mDialog != null) {
            mDialog.stopProgressDialog();
        }
        setData(mPageIndex, orderInfo.getList(), orderInfo.getDoc_url());
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPageIndex++;
                presenter.getOrder(mService, mPageIndex + "", mPageSize + "", "", "");
            }
        }, mRecyclerView);

    }

    @Override
    public void getOrderFail(int code, String msg) {
        if (mDialog != null) {
            mDialog.stopProgressDialog();
        }
        mSwipeLayout.setRefreshing(false);
    }

    private void setData(int pageIndex, List data, Map<String, String> map) {
        final int size = data == null ? 0 : data.size();
        if (pageIndex == 1) {
            mAdapter.setNewData(data, map);
            if (size == 0) {
                mAdapter.setEmptyView(mNotDataView);
            }
        } else {
            if (size > 0) {
                mAdapter.addData(data, map);
            }
        }
        if (size < mPageSize) {
            //第一页如果不够一页就不显示没有更多数据布局
            mAdapter.loadMoreEnd(false);
        } else {
            mAdapter.loadMoreComplete();
        }

    }

    /**
     * 订单操作成功，取消或者提交。
     */
    @Override
    public void orderOperationSuccess() {
        if(mODialog!=null){
            mODialog.stopProgressDialog();
        }
        if(mCancelDialog!=null){
            mCancelDialog.stopProgressDialog();
        }
        if (mDialog != null) {
            mDialog.stopProgressDialog();
        }
        if (mOrderStatus == 1) {
            TipUtils.showToast(App.getInstance(), "确认订单成功");
        } else {
            TipUtils.showToast(App.getInstance(), "取消订单成功");
        }
        presenter.getOrder(mService,"1", mPageSize + "", "", "");
        RxBus.get().post(new EventBean(Constants.EVENT_TYPE_ORDER_OPERATION));
    }

    @Override
    public void orderOperationFail(int code, String msg) {
        if(mODialog!=null){
            mODialog.stopProgressDialog();
        }
        if(mCancelDialog!=null){
            mCancelDialog.stopProgressDialog();
        }
        if (mDialog != null) {
            mDialog.stopProgressDialog();
        }
        TipUtils.showToast(App.getInstance(),msg);
    }
}
